package ioperation;

import book.Book;
import book.BookList;

import java.util.Scanner;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User:张熳
 * Date:2025-09-05
 * Time:15:45
 */

//写出你要添加的书名遍历数组，判断数组是否已经满了，如果没满，则将要增加的图书的信息补充完整，然后判断这本书在书架中是否已经存在，
// 不存在，则添加到数组当中，
//即书架新增了一本书，书架的书的个数加1
public class AddOperation implements IOperation{
    @Override
    public void work(BookList bookList) {
        System.out.println("增加图书📖");
        //判满
        int currentSize = bookList.getUsedSize();
        for (int i = 0; i < currentSize; i++) {
            if (currentSize == bookList.getBooks().length) {
                System.out.println("书架已经满了，无法增加📂！");
                return;
            }
        }
        //构建对象
        Scanner scanner = new Scanner(System.in);
        System.out.print("请输入你要增加的图书书名📕：");
        String name = scanner.nextLine();
        System.out.print("请输入要增加的图书作者👩：");
        String author = scanner.nextLine();
        System.out.print("请输入要增加的图书类型🖼️：");
        String type = scanner.nextLine();
        System.out.print("请输入要增加的图书价格💴：");
        int price = scanner.nextInt();

        Book newbook = new Book(name,author,price,type);

        for (int i = 0; i < currentSize; i++) {
            Book book = bookList.getBook(i);
            if (book.getName().equals(name)) {
                System.out.println("书架已有这本书，无需再添加📂！");
                return;
            }
        }
        //到这里添加图书
        bookList.setBook(currentSize,newbook);
        bookList.setUsedSize(currentSize+1);
        System.out.println("新增图书成功📕！");
    }
}
